package com.rtx.wms.std.job.sku;

import java.util.List;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.rtx.api.annotation.RtxJobAnnotation;
import com.rtx.api.quartz.job.BaseJob;
import com.rtx.wms.std.common.bean.WmsConstant;
import com.rtx.wms.std.common.service.CommonService;
import com.rtx.wms.std.sku.bean.po.Sku;
import com.rtx.wms.std.sku.handle.SKUSchedulerHandle;

@Component
@DisallowConcurrentExecution
@RtxJobAnnotation(jobName = "skutowmsjob",description = "sku同步到仓库级",cron = "0/10 * * * * ?")
public class SKUToWmsJob implements BaseJob {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SKUSchedulerHandle skuSchedulerHandle;

    @Autowired
    private CommonService commonService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
    	
        //激活的仓库
        List<String> whseidList = commonService.selectActiveWhseidNotEnterprise(WmsConstant.ENTERPRISE);
         
        List<Sku> list = skuSchedulerHandle.querySKU(WmsConstant.ENTERPRISE);
        
        for(Sku sku:list) {
            String msg ="OK";
            for(String whseid:whseidList) {
                try {
                	//设置默认分配策略 上架策略
                	skuSchedulerHandle.handleSKUSTRATEGY(whseid, sku);
                	//将sku 别名写入别名表
                	skuSchedulerHandle.handleAltesku(whseid, sku);
                	//将SKU分发到各个仓库
                	skuSchedulerHandle.distributeSKU(whseid,sku);
                } catch (Exception e) {
                    logger.error("仓库{}sku同步异常",whseid,e);
                    msg ="仓库"+whseid+"sku同步异常"+e.getMessage();
                }
            }

            skuSchedulerHandle.updStatus(WmsConstant.ENTERPRISE,msg, sku);
        }


    }
}
